package at.fhj.swd.pse.domain;

import static org.junit.Assert.assertEquals;

import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.data.NewsDAO;
import at.fhj.swd.pse.data.NewsDAOMock;
import at.fhj.swd.pse.entities.News;
import at.fhj.swd.pse.entities.User;

/**
 * @author Markus Prutsch
 **/

public class UTNewsService {

	NewsService m_newsService;
	NewsDAO m_newsDao;

	private News createNews(final String title, final String content) {
		News news = new News();
		User user = new User();
		Date timestamp = new Date();

		news.setId((long) 1);
		news.setTitle(title);
		news.setContent(content);
		news.setCreator(user);
		news.setTimestamp(timestamp);
		return news;
	}

	@Before
	public void setUp() {
		this.m_newsDao = new NewsDAOMock();
		this.m_newsService = new NewsServiceImpl(this.m_newsDao);
	}

	@Test
	public void testCreation() {
		News testNews = this.createNews("test", "test123");
		this.m_newsDao.insert(testNews);

		News t_news = this.m_newsService.insertNews(testNews);
		assertEquals(testNews, t_news);
	}

	@Test
	public void testUpdate() {
		News testNews = this.createNews("test", "test123");
		this.m_newsDao.insert(testNews);
		News testUpdateNews = this.createNews("test", "test124");
		this.m_newsDao.update(testUpdateNews);

		News t_updatednews = this.m_newsService.updateNews(testUpdateNews);
		assertEquals(testUpdateNews, t_updatednews);
	}

	@Test
	public void testOrderedNews() {
		News testAllNews = this.createNews("test", "test123");
		this.m_newsDao.insert(testAllNews);
		List<News> testAllNewsList = this.m_newsDao.getAllOrderedByCreation();

		List<News> t_allnews = this.m_newsService.getAllOrderedByCreation();
		assertEquals(testAllNewsList, t_allnews);
	}
}